// 2014-4-4上午9:04:45  net.betou.common.web.springmvc.WebResponse.java

package net.betou.common.web.springmvc;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.web.ResponseUtils;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.MessageSource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

/**
 * 
 * @author 忘记一切、重新开始
 */
public abstract class WebResponse{

	/**
	 * email正则表达式
	 */
	public static final Pattern EMAIL_PATTERN = Pattern.compile( "^\\w+(\\.\\w+)*@\\w+(\\.\\w+)+$" );
	/**
	 * username正则表达式
	 */
	public static final Pattern USERNAME_PATTERN = Pattern.compile( "^[0-9a-zA-Z\\u4e00-\\u9fa5\\.\\-@_]+$" );
	
	/**
	 * 成功消息
	 */
	public static final String SUCCESS = "success";
	/**
	 * 错误消息
	 */
	public static final String ERROR = "error";
	
	/**
	 * 警告消息
	 */
	public static final String WARN = "warn";
	
	

	/**
	 * 通过HttpServletRequest创建WebErrors
	 * 
	 * @param request
	 *            从request中获得MessageSource和Locale，如果存在的话。
	 */
	public WebResponse( HttpServletRequest request, HttpServletResponse response ) {
		WebApplicationContext webApplicationContext = RequestContextUtils.getWebApplicationContext( request );
		if( webApplicationContext != null ){
			LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver( request );
			Locale locale;
			if( localeResolver != null ){
				locale = localeResolver.resolveLocale( request );
				this.messageSource = webApplicationContext;
				this.locale = locale;
			}
		}
		this.response = response;
	}

	public WebResponse( ) {
	}

	/**
	 * 构造器
	 * 
	 * @param messageSource
	 * @param locale
	 */
	public WebResponse( MessageSource messageSource, Locale locale ) {
		this.messageSource = messageSource;
		this.locale = locale;
	}

	public String getMessage( String code, Object ... args ) {
		if( messageSource == null ){
			throw new IllegalStateException( "MessageSource cannot be null." );
		}
		return messageSource.getMessage( code, args, locale );
	}
	
	/**
	 * 添加具有国际化消息的响应信息
	 * @param type
	 * @param code
	 * @param args
	 */
	public void addMessageSourceResponse( String type, String code, Object ... args ){
		Map< String, String > responseMap = getResponse( );
		responseMap.put( "type", type );
		responseMap.put( "content", getMessage( code, args ) );
	}
	
	public void addResponse( String type, String response ){
		Map< String, String > responseMap = getResponse( );
		responseMap.put( "type", type );
		responseMap.put( "content", response );
	}

	/**
	 * 添加错误信息
	 * @param code
	 * @param args
	 */
	public void addErrorResponse( String code, Object ... args ){
		addMessageSourceResponse( ERROR, code, args );
	}
	
	/**
	 * 传递国际化参数
	 * @param code
	 */
	public void addErrorResponse( String code ){
		addMessageSourceResponse( ERROR, code );
	}

	public void addErrorResponseString( String error ){
		addResponse( ERROR, error );
	}
	
	public void addSuccessResponse( String code, Object ... args ){
		addMessageSourceResponse( SUCCESS, code, args );
	}
	
	public void addSuccessResponse( String code ){
		addMessageSourceResponse( SUCCESS, code );
	}

	public void addSuccessResponseString( String error ){
		addResponse( SUCCESS, error );
	}
	
	public void addWarnResponse( String code, Object ... args ){
		addMessageSourceResponse( WARN, code, args );
	}
	
	public void addWarnResponse( String code ){
		addMessageSourceResponse( WARN, code );
	}

	public void addWarnResponseString( String error ){
		addResponse( WARN, error );
	}
	
	public void showResponse( ){
		JSONObject obj = JSONObject.fromObject( getResponse( ) );
		ResponseUtils.renderJson( response, obj.toString( ) );
	}

	/**
	 * 是否存在消息
	 */
	public boolean hasResponse( ){
		return responseMap != null && responseMap.size( ) > 0;
	}

	public Map< String, String > getResponse( ){
		if( responseMap == null ){
			this.responseMap = new HashMap< String, String >( );
		}
		return responseMap;
	}

	

	public boolean ifNull( Object o ) {
		if( o == null ){
			return true;
		}else{
			return false;
		}
	}

	public boolean ifEmpty( Object [ ] o ) {
		if( o == null || o.length <= 0 ){
			return true;
		}else{
			return false;
		}
	}

	public boolean ifBlank( String s, int maxLength ) {
		if( StringUtils.isBlank( s ) ){
			return true;
		}
		if( ifMaxLength( s,  maxLength ) ){
			return true;
		}
		return false;
	}

	public boolean ifMaxLength( String s,  int maxLength ) {
		if( s != null && s.length( ) > maxLength ){
			return true;
		}
		return false;
	}

	public boolean ifOutOfLength( String s,  int minLength, int maxLength ) {
		if( s == null ){
			return true;
		}
		int len = s.length( );
		if( len < minLength || len > maxLength ){
			return true;
		}
		return false;
	}

	public boolean ifNotEmail( String email,  int maxLength ) {
		if( ifBlank( email,  maxLength ) ){
			return true;
		}
		Matcher m = EMAIL_PATTERN.matcher( email );
		if( !m.matches( ) ){
			return true;
		}
		return false;
	}

	public boolean ifNotUsername( String username,  int minLength, int maxLength ) {
		if( ifOutOfLength( username,  minLength, maxLength ) ){
			return true;
		}
		Matcher m = USERNAME_PATTERN.matcher( username );
		if( !m.matches( ) ){
			return true;
		}
		return false;
	}

	public boolean ifNotExist( Object o ) {
		if( o == null ){
			return true;
		}else{
			return false;
		}
	}

	private MessageSource messageSource;
	private Locale locale;
	private Map< String, String > responseMap;
	private HttpServletResponse response;

	public MessageSource getMessageSource( ) {
		return messageSource;
	}

	public void setMessageSource( MessageSource messageSource ) {
		this.messageSource = messageSource;
	}

	/**
	 * 获得本地化信息
	 * 
	 * @return
	 */
	public Locale getLocale( ) {
		return locale;
	}

	/**
	 * 设置本地化信息
	 * 
	 * @param locale
	 */
	public void setLocale( Locale locale ) {
		this.locale = locale;
	}
}
